<!--
  -    Copyright (c) 2018-2025, lengleng All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: lengleng (wangiegie@gmail.com)
  -->

<template>
  <div class="app-container calendar-list-container">
    <basic-container>
      <avue-crud
        ref="crud"
        :option="tableOption"
        :data="list"
        :page.sync="page"
        v-model="form"
        :table-loading="listLoading"
        @on-load="getList"
        @search-change="handleFilter"
        @refresh-change="handleRefreshChange"
        @row-update="update"
        @row-save="create">

        <template slot="menuLeft">
<!--          <el-button-->
<!--            v-if="permissions.sys_role_add"-->
<!--            class="filter-item"-->
<!--            type="primary"-->
<!--            icon="el-icon-edit"-->
<!--            @click="$refs.crud.rowAdd()">添加-->
<!--          </el-button>-->
        </template>
        <template slot="itemId" slot-scope="scope">
          <div v-html="scope.row.link"></div>
        </template>
        <template slot="type" slot-scope="scope">
          <el-tag>{{ scope.row.type }}</el-tag>
        </template>
        <template slot="flag" slot-scope="scope">
          <el-tag :type="scope.row.flag==1?'danger':'success'">{{ scope.row.flag==1?'待处理':'已处理' }}</el-tag>
        </template>

        <template
          slot="menu"
          slot-scope="scope">
          <el-button
            v-if="scope.row.flag == 1"
            type="text"
            style="color: #04b404"
            icon="el-icon-circle-check"
            size="small"
            @click="successHandle(scope.row)">通过
          </el-button>
          <el-button
            v-if="scope.row.flag == 1"
            type="text"
            style="color:red;"
            icon="el-icon-circle-close"
            size="small"
            @click="failHandle(scope.row)">不通过
          </el-button>
        </template>
      </avue-crud>
    </basic-container>
<!--    <el-dialog-->
<!--      :visible.sync="dialogPermissionVisible"-->
<!--      :close-on-click-modal="false"-->
<!--      title="分配权限">-->
<!--      <div class="dialog-main-tree">-->
<!--        <el-tree-->
<!--          ref="menuTree"-->
<!--          :data="treeData"-->
<!--          :default-checked-keys="checkedKeys"-->
<!--          :check-strictly="false"-->
<!--          :props="defaultProps"-->
<!--          :filter-node-method="filterNode"-->
<!--          class="filter-tree"-->
<!--          node-key="id"-->
<!--          highlight-current-->
<!--          show-checkbox-->
<!--          default-expand-all/>-->
<!--      </div>-->
<!--      <div slot="footer"-->
<!--           class="dialog-footer">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          @click="updatePermession(roleId)">更 新-->
<!--        </el-button>-->
<!--        <el-button-->
<!--          type="default"-->
<!--          @click="cancal()">取消-->
<!--        </el-button>-->
<!--      </div>-->
<!--    </el-dialog>-->
  </div>
</template>

<script>
import {addObj, delObj, fetchList, fetchRoleTree, permissionUpd, putObj} from '@/api/admin/role'
import {pageComplaint,updateComplaint} from '@/api/disk/complaint'
import {tableOption} from '@/const/crud/disk/complaint'
import {fetchMenuTree} from '@/api/admin/menu'
import {getShare} from '@/api/disk/share'
import {mapGetters} from 'vuex'

export default {
  name: 'complaint',
  data() {
    return {
      searchForm: {},
      tableOption: tableOption,
      treeData: [],
      checkedKeys: [],
      checkedDsScope: [],
      defaultProps: {
        label: 'name',
        value: 'id'
      },
      page: {
        order:'createTime',
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      menuIds: '',
      list: [],
      listLoading: true,
      form: {},
      roleId: undefined,
      roleCode: undefined,
      rolesOptions: undefined,
      dialogPermissionVisible: false,
    }
  },
  computed: {
    ...mapGetters(['elements', 'permissions'])
  },
  methods: {
    failHandle(item){

      this.$prompt('请输入不通过原因', '不通过处理', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(({ value }) => {

        item.flag = '3';
        item.handleMessage = value;
        console.log("=unpass=");
        console.log(item);

        updateComplaint(item).then(re=>{
          if (re.data.code == 0){
            this.$message.success("处理成功!");
            this.handleRefreshChange();
          }
        })

      });
    },
    successHandle(item){
      item.flag = '2';
      item.handleMessage = "通过,情况属实!";
      console.log("=pass=");
      console.log(item);
      this.$confirm('通过后，该分享文件将被禁止访问，是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {

        updateComplaint(item).then(re=>{
          if (re.data.code == 0){
            this.$message.success("处理成功!");
            this.handleRefreshChange();
          }
        })


      });
    },
    async getShareLink(shareId){

      if (shareId == null || shareId == "" || shareId == undefined){

        return "未知";
      }
      var share = null;
      await getShare(shareId).then(re=>{
         if (re.data.code == 0){
           share = re.data.data;
         }
      });
      console.log(share)
      if (share == null){
        return "未知";
      }else{
        return "<a type='primary' style='color: #2d8cf0;text-decoration: underline' href='http://127.0.0.1/#/share?path="+share.path+"&password="
          +share.password+"' target='_blank'>" +
          +share.path+ "</a>";
      }
    },
    getList(page, params) {
      this.listLoading = true
      pageComplaint(Object.assign({
        current: page.currentPage,
        size: page.pageSize,
        orderBy :'flag'
      }, params, this.searchForm)).then(response => {
        this.list = response.data.data.records
        this.page.total = response.data.data.total
        this.listLoading = false

        var _this = this;
        this.list.forEach(one => {
          _this.getShareLink(one.itemId).then(re=>{
            this.$set(one ,  'link',  re);
          })
        });
        console.log(this.list)
      }).catch(() => {
        this.listLoading = false
      })
    },
    handleRefreshChange() {
      this.getList(this.page)
    },
    handleFilter(form) {
      this.searchForm = form
      this.page.currentPage = 1
      this.getList(this.page, form)
    },
    handleUpdate(row, index) {
      this.$refs.crud.rowEdit(row, index)
    },
    cancal() {
      this.dialogPermissionVisible = false;
    },
    handlePermission(row) {
      fetchRoleTree(row.roleId)
        .then(response => {
          this.checkedKeys = response.data.data
          return fetchMenuTree()
        })
        .then(response => {
          this.treeData = response.data.data
          // 解析出所有的太监节点
          this.checkedKeys = this.resolveAllEunuchNodeId(this.treeData, this.checkedKeys, [])
          this.dialogPermissionVisible = true
          this.roleId = row.roleId
          this.roleCode = row.roleCode
        })
    },
    /**
     * 解析出所有的太监节点id
     * @param json 待解析的json串
     * @param idArr 原始节点数组
     * @param temp 临时存放节点id的数组
     * @return 太监节点id数组
     */
    resolveAllEunuchNodeId(json, idArr, temp) {
      for (let i = 0; i < json.length; i++) {
        const item = json[i]
        // 存在子节点，递归遍历;不存在子节点，将json的id添加到临时数组中
        if (item.children && item.children.length !== 0) {
          this.resolveAllEunuchNodeId(item.children, idArr, temp)
        } else {
          temp.push(idArr.filter(id => id === item.id))
        }
      }
      return temp
    },
    filterNode(value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    getNodeData(data, done) {
      done()
    },
    handleDelete(row, index) {
      var _this = this
      this.$confirm('是否确认删除名称为"' + row.roleName + '"' + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function () {
        return delObj(row.roleId)
      }).then(() => {
        this.getList(this.page)
        this.$notify.success('删除成功')
      })
    },
    create(row, done, loading) {
      if (this.form.dsType === 1) {
        this.form.dsScope = this.$refs.scopeTree.getCheckedKeys().join(',')
      }
      addObj(this.form).then(() => {
        this.getList(this.page)
        done()
        this.$notify.success('创建成功')
      }).catch(() => {
        loading()
      })
    },
    update(row, index, done, loading) {
      if (this.form.dsType === 1) {
        this.form.dsScope = this.$refs.scopeTree.getCheckedKeys().join(',')
      }
      putObj(this.form).then(() => {
        this.getList(this.page)
        done()
        this.$notify.success('修改成功')
      }).catch(() => {
        loading()
      })
    },
    updatePermession(roleId) {
      this.menuIds = ''
      this.menuIds = this.$refs.menuTree.getCheckedKeys().join(',').concat(',').concat(this.$refs.menuTree.getHalfCheckedKeys().join(','))
      permissionUpd(roleId, this.menuIds).then(() => {
        this.dialogPermissionVisible = false
        this.$store.dispatch('GetMenu', {type: false})
        this.$notify.success('修改成功')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-dialog__wrapper {
  .el-dialog {
    width: 61% !important;

    .dialog-main-tree {
      max-height: 400px;
      overflow-y: auto;
    }
  }

  .el-form-item__label {
    width: 20% !important;
    padding-right: 20px;
  }

  .el-form-item__content {
    margin-left: 20% !important;
  }
}
</style>

